package com.css.fxfzfxqh.modules.buildingIndividual.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzfxqh.base.dict.service.DictItemService;
import com.css.fxfzfxqh.base.dict.viewobjects.DictEntityVo;
import com.css.fxfzfxqh.modules.buildingIndividual.entity.BuildingIndividualEntity;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class VerifyHandler implements IExcelVerifyHandler<BuildingIndividualEntity> {

    @Autowired
    DictItemService dictItemService;

    @Override
    public ExcelVerifyHandlerResult verifyHandler(BuildingIndividualEntity buildingIndividualEntity) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getProvince())) {
            result.setSuccess(false);
            message.append("省份 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getCity())) {
            result.setSuccess(false);
            message.append("市 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getCounty())) {
            result.setSuccess(false);
            message.append("区/县级 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getLongitude())) {
            result.setSuccess(false);
            message.append("经度 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getLatitude())) {
            result.setSuccess(false);
            message.append("纬度 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getStructureType1())) {
            result.setSuccess(false);
            message.append("结构类型一级 不能为空、");
        }else{
            String structureType1 = buildingIndividualEntity.getStructureType1();
            if (structureType1.equals("砌体结构") || structureType1.equals("钢筋混凝土结构") || structureType1.equals("工业厂房")) {
                if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getStructureType2())) {
                    result.setSuccess(false);
                    message.append("结构类型二级 不能为空、");
                }else{
                    //校验二级的值是否属于一级
                    String structureType2 = buildingIndividualEntity.getStructureType2();
                    if(structureType1.equals("砌体结构")){
                        List<DictEntityVo> qtjgej = dictItemService.getDictItemByDictCode("qtjgej");
                        if(!verify(qtjgej,structureType2)){
                            result.setSuccess(false);
                            message.append("二级结构类型的值不属于砌体结构、");
                        }
                    }else if(structureType1.equals("钢筋混凝土结构")){
                        List<DictEntityVo> qtjgej = dictItemService.getDictItemByDictCode("gjhntjgej");
                        if(!verify(qtjgej,structureType2)){
                            result.setSuccess(false);
                            message.append("二级结构类型的值不属于钢筋混凝土结构、");
                        }
                    }else if(structureType1.equals("工业厂房")){
                        List<DictEntityVo> qtjgej = dictItemService.getDictItemByDictCode("gycfej");
                        if(!verify(qtjgej,structureType2)){
                            result.setSuccess(false);
                            message.append("二级结构类型的值不属于工业厂房、");
                        }
                    }
                }
            }
        }
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getDefenceLevel())) {
            result.setSuccess(false);
            message.append("设防情况 不能为空、");
        }
        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getUsage1())) {
            result.setSuccess(false);
            message.append("房屋用途一级 不能为空、");
        }else {
            String usage1 = buildingIndividualEntity.getUsage1();
            if(usage1.equals("第Ⅰ类") || usage1.equals("第Ⅱ类")){
                if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getUsage2())) {
                    result.setSuccess(false);
                    message.append("房屋用途二级 不能为空、");
                }else{
                    //校验二级的值是否属于一级
                    String usage2 = buildingIndividualEntity.getUsage2();
                    if(usage1.equals("第Ⅰ类")){
                        List<DictEntityVo> qtjgej = dictItemService.getDictItemByDictCode("dylej");
                        if(!verify(qtjgej,usage2)){
                            result.setSuccess(false);
                            message.append("二级房屋用途的值不属于第Ⅰ类、");
                        }
                    }else if(usage1.equals("第Ⅱ类")){
                        List<DictEntityVo> qtjgej = dictItemService.getDictItemByDictCode("delej");
                        if(!verify(qtjgej,usage2)){
                            result.setSuccess(false);
                            message.append("二级房屋用途的值不属于第Ⅱ类、");
                        }
                    }
                }
            }
        }

        if (PlatformObjectUtils.isEmpty(buildingIndividualEntity.getArea())) {
            result.setSuccess(false);
            message.append("建筑面积 不能为空、");
        }

        String mess = message.toString();
        if (!PlatformObjectUtils.isEmpty(mess)) {
            mess = mess.substring(0,mess.length()-1)+"。";
        }
        result.setMsg(mess);
        return result;
    }
    //校验二级的值是否属于一级
    private boolean verify(List<DictEntityVo> list, String str){
        boolean res = false;
        for(DictEntityVo dictEntityVo : list){
            String dictItemName = dictEntityVo.getDictItemName();
            if(dictItemName.equals(str)){
                res = true;
                break;
            }
        }
        return res;
    }

}
